<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class Appointment extends Model
{

    

    

    // 表名
    protected $name = 'appointment';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'technician_text'
    ];
    

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTechnicianTextAttr($value,$data)
    {
        $value = $value ? $value : (isset($data['technician']) ? $data['technician'] : '');
        $technicianArr = explode(',', $data['technician_ids']);
                //     // dump($technicianArr);
        $technicians = Db::name('technician')->where('id', 'in', $technicianArr)->column('name');

        $technician = implode(',', array_values($technicians));
        return $technician;
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function shop()
    {
        return $this->belongsTo('Shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function house()
    {
        return $this->belongsTo('House', 'house_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function technician()
    {
        return $this->belongsTo('Technician', 'technician_ids', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function project()
    {
        return $this->belongsTo('Project', 'project_ids', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
